#!/usr/bin/env python

import sys
import myupnp
try:
 	import pygtk
  	pygtk.require("2.0")
except:
  	pass
try:
	import gtk
  	import gtk.glade
except:
	sys.exit(1)

class myController:
	"""This is an Hello World GTK application"""

	def __init__(self):
		builder = gtk.Builder()
		builder.add_from_file("gtk/sPyCo.ui")
		builder.connect_signals(self);

		window			  = builder.get_object("window")
		fixed 			  = builder.get_object("fixed")
		self.VolumeButton = builder.get_object("volume")

		window.set_title("GsPyCo")
		BtnPrev = builder.get_object("prev")

		box = gtk.HBox(homogeneous=True, spacing=10)

		self.ComboServer   = gtk.combo_box_new_text()
		self.ComboRenderer = gtk.combo_box_new_text()
		self.ComboServer.append_text('Select a MediaServer:')
		self.ComboRenderer.append_text('Select a MediaRenderer:')

		self.ComboRenderer.connect('changed', self.on_renderer_changed)

		self.Renderer = myupnp.init_MediaRenderer()
		self.Server = myupnp.init_MediaServer()

		for i in self.Renderer:	self.ComboRenderer.append_text(i.name)
		for i in self.Server:	self.ComboServer.append_text(i.name)

		box.pack_start(self.ComboServer)
		box.pack_end(self.ComboRenderer)

		fixed.add(box)
		window.show_all()

	def on_volume_changed(self, widget, desired_volume):
		#print "desired volume ", desired_volume
		it = self.ComboRenderer.get_active() -1
		cv = self.Renderer[it].SetVolume(desired_volume * 100.);
		#print "current volume:", self.Renderer[it].GetVolume()

	def on_renderer_changed(self, widget):
		print "New renderer:", self.ComboRenderer.get_active(), widget
		it = self.ComboRenderer.get_active() -1
		cv = self.Renderer[it].GetVolume();
		#print "current volume:", self.Renderer[it].GetVolume()
		self.VolumeButton.set_value(cv /100.);

	def destroy(self,window):
		gtk.main_quit()

if __name__ == "__main__":
	hwg = myController()
	gtk.main()
